package com.fjzx.androidframe.base.adapter;

import android.support.annotation.NonNull;
import android.support.v7.recyclerview.extensions.AsyncListDiffer;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.fjzx.androidframe.base.annotation.injector.ItemViewInjector;
import com.fjzx.androidframe.base.callback.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 基础RecylerView适配器
 *
 * @param <T>
 * @author xiongyongcong
 */
public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<BaseRecyclerViewAdapter<T>.BaseRvViewHolder> {
    /**
     * 布局解析器，申明为成员变量，防止每次都去初始化一次
     **/
    protected LayoutInflater mInflater;
    /**
     * Item点击事件
     **/
    protected OnItemClickListener mClickListener;

    protected AsyncListDiffer<T> mDiffer;

    public BaseRecyclerViewAdapter() {
        mDiffer = new AsyncListDiffer(this, new DiffUtil.ItemCallback() {
            @Override
            public boolean areItemsTheSame(Object oldItem, Object newItem) {
                return oldItem.equals(newItem);
            }

            @Override
            public boolean areContentsTheSame(Object oldItem, Object newItem) {
                return oldItem.equals(newItem);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDiffer.getCurrentList().size();
    }

    @Override
    public void onBindViewHolder(BaseRvViewHolder holder, int position) {
        // 子类实现绑定数据的方法
        bindData(holder, position, getItem(position));
    }

    @Override
    public BaseRvViewHolder onCreateViewHolder(ViewGroup parent, int itemViewType) {
        if (mInflater == null) {
            mInflater = LayoutInflater.from(parent.getContext());
        }
        View itemView = mInflater.inflate(createItemViewId(parent, itemViewType), parent, false);
        return new BaseRvViewHolder(itemView);
    }

    protected int createItemViewId(ViewGroup parent, int itemViewType) {
        return ItemViewInjector.inject(this);
    }

    /**
     * 设置Item点击事件
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mClickListener = listener;
    }

    public T getItem(int position) {
        return mDiffer.getCurrentList().get(position);
    }

    /**
     * 绑定数据
     *
     * @param data
     */
    public void submitList(final List<T> data) {
        mDiffer.submitList(data);
    }

    /**
     * 添加数据
     *
     * @param data
     * @param addToFront 是否从List前面添加
     */
    public void addData(T data, boolean addToFront) {
        List<T> currentList = new ArrayList<>();
        if (addToFront) {
            currentList.add(data);
            currentList.addAll(mDiffer.getCurrentList());
        } else {
            currentList.addAll(mDiffer.getCurrentList());
            currentList.add(data);
        }
        submitList(currentList);
    }

    /**
     * 清除数据
     */
    public void clearData() {
        mDiffer.submitList(null);
    }

    public List<T> getData() {
        return mDiffer.getCurrentList();
    }

    /**
     * 子类实现此方法，完成数据绑定
     *
     * @param holder   ViewHolder
     * @param position Position
     * @param data     数据
     */
    abstract protected void bindData(@NonNull BaseRvViewHolder holder, int position, @NonNull T data);

    /**
     * 基础RecyclerView ViewHolder类
     */
    public class BaseRvViewHolder extends ViewHolder implements OnClickListener {
        public SparseArray<View> mViews = new SparseArray<>();

        public BaseRvViewHolder(View itemView) {
            super(itemView);
            // 设置Item的点击事件
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) {
                mClickListener.onItemClick(view, getLayoutPosition());
            }
        }

        /**
         * 获取Item的子View
         *
         * @param id  view id
         * @param <V> View实例
         * @return
         */
        @SuppressWarnings("unchecked")
        public <V extends View> V getView(int id) {
            View view = mViews.get(id);
            if (view == null) {
                view = itemView.findViewById(id);
                mViews.put(id, view);
            }
            return (V) view;
        }
    }
}
